package com.kmdd.xtds.ui

import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.kmdd.xtds.MainActivity
import com.kmdd.xtds.configs.PrefsManager
import com.kmdd.xtds.configs.UrlConfigs
import com.kmdd.xtds.databinding.ActivitySplashBinding
import com.kmdd.xtds.view.SplashAgreementDialog
import com.gyf.immersionbar.ktx.immersionBar

class SplashActivity : AppCompatActivity() {
    private lateinit var binding: ActivitySplashBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivitySplashBinding.inflate(layoutInflater)
        setContentView(binding.root)
        immersionBar {
            transparentBar()
            keyboardEnable(true)
            fitsSystemWindows(false)
            statusBarDarkFont(false)
        }
        initView()
    }

    private fun initView(){
        if (PrefsManager.getAgreement()) {
            start()
        } else {
            val splashAgreementDialog = SplashAgreementDialog.newInstance()
            splashAgreementDialog.setOnClickButtonListener(object :
                SplashAgreementDialog.onClickButtonListener {
                override fun onClickConfirm() {
                    PrefsManager.saveAgreement(true)
                    start()
                }

                override fun onClickCancel() {
                    PrefsManager.saveAgreement(false)
                    finish()
                }

                override fun onClickPrivacy() {
                    //隐私协议
                    WebActivity.start(this@SplashActivity, UrlConfigs.PRIVACY_URL)
                }

                override fun onClickLicence() {
                    //用户协议
                    WebActivity.start(this@SplashActivity, UrlConfigs.USER_URL)
                }

            })
            splashAgreementDialog.show(supportFragmentManager, "SplashAgreementDialog")
        }
    }

    private fun start(){
        startActivity(Intent(this, MainActivity::class.java))
        finish()
    }

}